package com.cai.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cai.constant.MessageConstant;
import com.cai.dao.MemberDao;
import com.cai.dao.OrderDao;
import com.cai.dao.OrderSettingDao;
import com.cai.entity.Result;
import com.cai.pojo.Member;
import com.cai.pojo.Order;
import com.cai.pojo.OrderSetting;
import com.cai.service.OrderService;
import com.cai.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 添加预约
     * @param map
     * @return
     * @throws Exception
     */
    public Result order(Map map) throws Exception {
        //检查用户预约的日期是否人数已满
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number = orderSetting.getNumber();//获取可预约人数
        int reservations = orderSetting.getReservations();//获取已预约人数

        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //查询该用户是否为会员
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {//该用户已经是会员
            Integer memberId = member.getId();
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
            List<Order> orders = orderDao.findByCondition(new Order(memberId, date, setmealId));
            if (orders != null && orders.size() > 0) {
                //已经完成预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else { //该用户不是会员,注册会员

            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //保存预约信息到预约表
        Order order = new Order(member.getId(),
                date,
                (String) map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String)
                        map.get("setmealId")));
        orderDao.add(order);

        //更新order表中已预约的人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());

    }

    //根据id查询预约套餐信息
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }

}
